﻿using System.Collections.Generic;
using System.IO;
using System.Web;
using SouthernLand.Common.DAL.Photos;

namespace SouthernLand.Common.BOL.Photo
{
    public class PhotoCategory
    {
        #region "======Properties====="

        public int CateID { get; set; }

        public string AppKey { get; set; }

        public string CateName { get; set; }

        public string CateDesc { get; set; }

        public string CateName_en { get; set; }

        public string CateDesc_en { get; set; }

        public string ImageUrl { get; set; }

        public int CateOrder { get; set; }

        public int ParentCateID { get; set; }

        #endregion

        #region "==== Method ===="

        // lay Albums dau tien
        public static int GetTop1Albums()
        {
            List<PhotoCategory> obj = GetAlbums();
            foreach (PhotoCategory fcate in obj)
            {
                return fcate.CateID;
            }
            return -1;
        }

        // lay duong dan cua file hinh dai dien cho Albums
        public static string GetPathByCateID(int CateID)
        {
            var db = new PhotoCategoryDAL();
            string path = HttpContext.Current.Server.MapPath("~/Gallery/");
            PhotoCategory obj = db.GetCateByCateID(CateID);
            if (obj.ImageUrl != null && obj.ImageUrl != "")
                path = path + obj.AppKey + "\\" + obj.ImageUrl;
            else
                path = "";
            return path;
        }

        public static string GetAppKeyByCateID(int CateID)
        {
            var db = new PhotoCategoryDAL();
            return db.GetAppKeyByCateID(CateID);
        }

        public static bool CheckAppKey(string AppKey)
        {
            var db = new PhotoCategoryDAL();
            return db.CheckAppKey(AppKey);
        }

        // lay Albums 
        public static List<PhotoCategory> GetAlbums()
        {
            var db = new PhotoCategoryDAL();
            return db.GetAlbums();
        }

        public static List<PhotoCategory> GetCateByParentCateID(int ParentCateID)
        {
            var db = new PhotoCategoryDAL();
            return db.GetCateByParentCateID(ParentCateID);
        }

        public static PhotoCategory GetCateByCateID(int CateID)
        {
            var db = new PhotoCategoryDAL();
            return db.GetCateByCateID(CateID);
        }

        public static int Insert(PhotoCategory obj)
        {
            var db = new PhotoCategoryDAL();
            return db.Insert(obj);
        }

        public static int Update(PhotoCategory obj)
        {
            var db = new PhotoCategoryDAL();
            return db.Update(obj);
        }

        public static int Delete(int CateID)
        {
            var db = new PhotoCategoryDAL();
            PhotoCategory obj = db.GetCateByCateID(CateID);
            if (Directory.Exists(Globals.GetGalleryPath() + obj.AppKey) && (obj.AppKey.Trim() != ""))
                Directory.Delete(Globals.GetGalleryPath() + obj.AppKey, true);

            return db.Delete(CateID);
        }

        #endregion
    }
}